#include "chessplay2.h"
#include "ui_chessplay2.h"
#include<QPoint>
#include<QPainter>
#include<QFont>
#include<QMouseEvent>
#include<QLabel>
#include<QMouseEvent>
#include<QPushButton>
#include<QMessageBox>
#include<QBrush>
#include<QVector>
#include<QDebug>
#include<QPen>
#include<QBrush>
#include<QStack>
#include<QColor>
#include<QRect>
#include"inter.h"
chessplay2::chessplay2(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::chessplay2)
{
    ui->setupUi(this);

    //窗口标题
    setWindowTitle("五子棋游戏世界欢迎您");
    setMinimumSize(850,650);
    setMaximumSize(850,650);
    //游戏开始按钮
    button=new QPushButton(this);
    button->setGeometry(670,200,150,50);
    button->setText("开始游戏");
   button->setStyleSheet("QPushButton{border-image:url(:/background/image/f2e692c7f646fc46b14102a50dc2682.jpg)}");
    connect(button,SIGNAL(clicked()),this,SLOT(open()));
    //返回主界面
    QPushButton*backbutton=new QPushButton(this);
    backbutton->setGeometry(670,100,150,50);
    backbutton->setText("返回");
    backbutton->setStyleSheet("QPushButton{border-image:url(:/background/image/f2e692c7f646fc46b14102a50dc2682.jpg)}");

        connect(backbutton,SIGNAL(clicked()),this,SLOT(mainback()));
    //字幕
    QLabel *label=new QLabel(this);
    label->setText("人生如棋");
    label->setGeometry(680,440,150,50);
    label->setStyleSheet("QLabel{color:red;font-size:30px}");
    QLabel *label1=new QLabel(this);
    label1->setText("落子无悔");
    label1->setGeometry(690,520,150,50);
    label1->setStyleSheet("QLabel{color:#FF2000;font-size:30px}");
    //悔棋按钮
    button1 = new QPushButton(this);
    button1->setGeometry(670,300,150,50);
    button1->setText("悔棋");
    button1->setStyleSheet("QPushButton{border-image:url(:/background/image/f2e692c7f646fc46b14102a50dc2682.jpg)}");

    connect(button1,SIGNAL(clicked()),this,SLOT(back()));

}


chessplay2::~chessplay2()
{
    delete ui;
}
void chessplay2::open(){
    int i=0;
    qDebug()<<"game started!";

    chessplay2*c=new chessplay2();

    if(i%2!=0){
        i++;
        this->clearFocus();
        c->show();

    }
    else c->close();


}
void chessplay2::mainback(){
    this->close();

    inter *s=new inter();
    s=new inter();
    s->show();

}

void chessplay2::prepare()
{



    handchess_color = false; // false -> 黑色 true -> 白色
}
void chessplay2::drawchessboard()
{ QPainter painter(this);
    painter.setPen(QPen(QColor(Qt::black),1));//绘制边框线颜色和大小

    painter.setBrush(QColor(213,176,141));//格子颜色
    for(int i = 0;i < col;i++)
        for(int j = 0;j < row;j++)
            painter.drawRect((0.5+i)*xsize,(0.5+j)*ysize,xsize,ysize);//绘制棋盘


}

void chessplay2::drawchess()
{
    QPainter painter(this);
    painter.setPen(QPen(QColor(Qt::transparent)));//给棋子边框设置颜色
    if(handchess_color == true)

        painter.setBrush(Qt::white);
    else
        painter.setBrush(Qt::black);
    //获得鼠标单击点的全局位置，长度，宽度
    painter.drawEllipse(mapFromGlobal(QCursor::pos()),xsize/2,ysize/2);



}
//画棋子
void chessplay2::drawchess1()
{
    QPainter painter(this);
    painter.setPen(QPen(QColor(Qt::transparent)));
    for(int i = 0;i < chessItem.size();i++)
    {
        chessitem item = chessItem[i];
        if(item.color == true)
        {
            painter.setBrush(Qt::white);
        }
        else
        {
            painter.setBrush(Qt::black);
        }
        chessplace(painter,item.point);
    }

}
void chessplay2::paintEvent(QPaintEvent *)

{
    int count=0;
    if(count==0)  {  QPainter painter(this);
    QPixmap pixmap(":/background/image/63c580f3b99ae1673887987225.jpg");

        painter.drawPixmap(0,0,850,650,pixmap);
    count++;
    }

    drawchessboard();
    drawchess();
    drawchess1();
    update();//更新后paintEvent函数被调用


}



void chessplay2::chessplace(QPainter &painter, QPoint &point)
{
    QPoint ptCenter((point.x() + 0.5) * xsize,(point.y() + 0.5) * ysize);
    painter.drawEllipse(ptCenter,xsize/2,ysize/2);
}

void chessplay2::mousePressEvent(QMouseEvent *event)
{
    QPoint pt;
    if(event->pos().x()/xsize >= 16)
        return;
    pt.setX(event->pos().x()/xsize);
    pt.setY(event->pos().y()/ysize);
    qDebug()<<"x:"<<event->pos().x()/xsize<<"y:"<<event->pos().y()/ysize<<' ';
    for(int i = 0;i <chessItem.size();i++)
    {
        chessitem item = chessItem[i];
        if(item.point == pt)
            return;
    }

    chessitem item(pt,handchess_color);
    chessItem.append(item);//添加元素

    int left = checkchess(item,QPoint(-1,0));
    int right = checkchess(item,QPoint(1,0));
    int leftup = checkchess(item,QPoint(-1,-1));
    int rightup = checkchess(item,QPoint(1,-1));
    int leftdown = checkchess(item,QPoint(-1,1));
    int rightdown = checkchess(item,QPoint(1,1));
    int up = checkchess(item,QPoint(0,-1));
    int down = checkchess(item,QPoint(0,1));


    if((left + right)>= 4 || (up + down)>=4 ||(leftup+rightdown)>=4 || (leftdown+rightup)>=4)
    {
        QString str = handchess_color ?"白棋胜利！！！" : "黑棋胜利！！！";
        QMessageBox::information(NULL,"Game Over",str,QMessageBox::Yes);
        chessItem.clear();
        handchess_color=false;
    }
    handchess_color = !handchess_color;
}

int chessplay2::checkchess(chessitem item,QPoint pt)
{
    int count = 0;
    item.point += pt;
    while(chessItem.contains(item)){
        count++;
        item.point+=pt;


    }

    return count;
}

//悔棋
void chessplay2::back()
{
    if(!chessItem.empty())
    {

        chessitem item = chessItem.last();
        qDebug() << item.point << ' ';
        chessItem.removeLast();

    }
    else
        qDebug()<<"empty";
}
